package cclub.demo.service;

import cclub.demo.exception.InvalidParameterException;
import cclub.demo.exception.SysException;
import cclub.demo.mapper.InterviewProjectMapper;
import cclub.demo.pojo.DO.interviewProjectDO;
import cclub.demo.service.intf.interviewProjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class interviewProjectServiceImpl extends ServiceImpl<InterviewProjectMapper, interviewProjectDO>
        implements interviewProjectService{

    @Autowired
    private InterviewProjectMapper mapper;

    @Override
    public void placeOnFile(Integer projectId) {
        interviewProjectDO projectDO = new interviewProjectDO();
        projectDO.setPlaceOnFile(new Date());
        this.updateById(projectDO,projectId);
    }

    @Override
    public void topping(Integer projectId,boolean toppingStatus) {
        interviewProjectDO projectDO = new interviewProjectDO();
        projectDO.setToppingTime(toppingStatus?System.currentTimeMillis():0);
        this.updateById(projectDO,projectId);
    }

    private void updateById(interviewProjectDO projectDO,Integer projectId){
        UpdateWrapper<interviewProjectDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",projectId);
        try {
            int update = mapper.update(projectDO,updateWrapper);
            if(update == 0){
                throw new InvalidParameterException("参数异常,更新无效");
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new SysException("系统更新面试项目异常");
        }
    }

    @Override
    public IPage<interviewProjectDO> queryInterviewProject(Integer userId, Integer page, Integer num, boolean projectStatus) {
        QueryWrapper<interviewProjectDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        if(projectStatus){
            queryWrapper.isNull("place_on_file");
            queryWrapper.last("order by IF(ISNULL(topping_time),1,0),topping_time ASC");
        }else{
            queryWrapper.isNotNull("place_on_file");
            queryWrapper.orderByAsc("place_on_file");
        }
        IPage<interviewProjectDO> iPage = new Page<>(page,num);
       return mapper.selectPage(iPage,queryWrapper);
    }

    @Override
    public void removeInterviewProject(Integer projectId) {
        try {
            int remove = mapper.deleteById(projectId);
            if(remove == 0){
                throw new InvalidParameterException("删除面试项目传递的id不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new SysException("系统更新面试项目异常");
        }
    }
}
